//
//  WH_AddressViewController.m
//  App_6.23
//
//  Created by wanghao on 16/7/20.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_AddressViewController.h"
#import "WH_AddViewController.h"
#import "WH_AddressCell.h"
#import "WH_Address.h"

@interface WH_AddressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myDataArr;
@property (nonatomic, strong) UIImageView *imageTitle;

@end

@implementation WH_AddressViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self postData];
    [self.tabBarController.tabBar setHidden:YES];

}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonAction)];
    
    [self createMyTable];
    self.myDataArr = [NSMutableArray array];
    
}
-(void)addButtonAction{
    
    WH_AddViewController *addVC = [[WH_AddViewController alloc] init];
    addVC.UrlStr = @"address_add";
    [self.navigationController pushViewController:addVC animated:YES];
    
}
- (void)createAddBtn{
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.tag = 100861;
    [self.view addSubview:addBtn];
    addBtn.backgroundColor = [UIColor colorWithRed:0.86 green:0.21 blue:0.32 alpha:1];
    [addBtn addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setTitle:@"添加地址" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.layer.cornerRadius = 5;
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(@-10);
        make.height.equalTo(@40);
    }];

}
- (void)deleteAddBtn{
    
    UIButton *button = (UIButton *)[self.view viewWithTag:100861];
    [button removeFromSuperview];
}
- (void)createMyTable{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 90;
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"WH_AddressCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"addressCell"];
    [self.view addSubview:self.myTableView];
    self.myTableView.scrollEnabled = NO;
    [self.myTableView setHidden:YES];
    self.myTableView.tableFooterView = [UIView new];

}
-(void)postData{
    
    [self.myDataArr removeAllObjects];
    
    NSString *body = [NSString stringWithFormat:@"action=address&user_id=%@",NSUSERID];
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSDictionary *address_array = [dic objectForKey:@"address_array"];
        for (NSDictionary *dic in address_array) {
            
            WH_Address *address = [WH_Address modelWithDictionary:dic];
            [self.myDataArr addObject:address];
        }
        if (_myDataArr.count == 0) {
            
            if ([self.view viewWithTag:100861]) {
                [self deleteAddBtn];
            }
            
            [self createAddBtn];
            [self.myTableView setHidden:YES];
        }else{
            [self.myTableView setHidden:NO];
            [self deleteAddBtn];
            
        }
        
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.myDataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *addressCell = @"addressCell";
    WH_AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:addressCell];
    cell.myAddress = self.myDataArr[indexPath.row];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WH_AddViewController *addVC = [[WH_AddViewController alloc] init];
    addVC.editAddress = self.myDataArr[indexPath.row];
    addVC.UrlStr = @"address_edit";
    [self.navigationController pushViewController:addVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
